package com.sina.shop.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 */
public class Validator {

    /**
     * 验证邮箱是否合法
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@"
                + "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

        Pattern pat = Pattern.compile(emailRegex);
        Matcher matcher = pat.matcher(email);
        return matcher.matches();
    }

    /**
     * 验证手机号码是否合法
     *
     * @param mobile
     * @return
     */
    public static boolean isValidMobile(String mobile) {
        String mobileRegex = "^\\+?[0-9]{1,3}\\-?[0-9]{10,11}$";

        Pattern pat = Pattern.compile(mobileRegex);
        Matcher matcher = pat.matcher(mobile);
        return matcher.matches();
    }

    /**
     * 验证密码是否合法
     *
     * @param password
     * @return
     */
    public static boolean isValidPassword(String password) {
        String passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$";

        Pattern pat = Pattern.compile(passwordRegex);
        Matcher matcher = pat.matcher(password);
        return matcher.matches();
    }

    /**
     * 验证是否为空
     *
     * @param str
     * @return
     */
    public static boolean isNull(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        }
        return false;
    }

    /**
     * 验证是否为整数
     *
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 验证长度是否在范围内
     *
     * @param str
     * @param min
     * @param max
     * @return
     */
    public static boolean isLengthInRange(String str, int min, int max) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        if (length < min || length > max) {
            return false;
        }
        return true;
    }

    /**
     * 验证是否为数字
     * @param str
     * @return
     */
    public static boolean isNumber(String str){
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

}
